// pages/allGoods/allGoods.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentSelect: 100,
    allTabs: [],
    currentGoodsList: [],
    allGoodsLists: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          userInfo: wx.getStorageSync("userInfo"),
          userTypeName: wx.getStorageSync("userTypeName"),
          imageUrl: app.globalData.imageUrl,
          userType: wx.getStorageSync("userType")
        });
      }
    });
    getClassify(that, 100)
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getAllGoodsByClassify(this)
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  switchTab: function (e) {
    this.setData({
      currentSelect: e.target.id
    })
    if (e.target.id == 100) {
      this.setData({
        currentGoodsList: this.data.allGoodsLists
      })
    } else {
      if (!this.data.allTabs[e.target.dataset.index].picture) {
        getGoodsByClassify(this, e.target.id, e.target.dataset.index)
      } else {
        this.setData({
          currentGoodsList: this.data.allTabs[e.target.dataset.index].picture
        })
      }
    }

  }
})

function getClassify(that, id) {
      wx.request({
        url: app.globalData.allUrl + "/superMarketAction/getGoodsType",
        data: {
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          that.setData({
            allTabs: res.data,
          })
        }
      })
}

function getGoodsByClassify(that, id, index) {
    wx.request({
      url: app.globalData.allUrl + "/directAction/getDirectProduct",
      data: {
      //  directId: 1,
        pageNum: 1,
        pageCount: 30,
        classify: id,
        putaway: 1
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        that.data.allTabs[index].picture = res.data.list;
        that.setData({
          allTabs: that.data.allTabs,
          currentGoodsList: res.data.list
        })
      }
    })
}

function getAllGoodsByClassify(that) {
  wx.request({
    url: app.globalData.allUrl + "/directAction/getDirectProduct",
    data: {
      directId: 1,
      pageNum: 1,
      pageCount: 30,
      putaway: 1
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      that.setData({
        allGoodsLists: res.data.list,
        currentGoodsList: res.data.list,
      })
    }
  })

}